package com.yx.test3d.model;

import android.content.Context;
import android.opengl.GLES30;
import android.util.Log;


import com.yx.test3d.utils.ShaderUtil;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;


public class Triangle extends Model {

/*    private static final String TAG = Triangle.class.getSimpleName();
    private static final int VERTEX_SIZE = 3;
    private static final int COLOR_SIZE = 3;
    private static int LOCATION_VERTEX = 0;
    private static int LOCATION_COLOR = 1;
    private static int LOCATION_MATRIX_M;
    private static int LOCATION_MATRIX_V;
    private static int LOCATION_MATRIX_P;
    protected FloatBuffer vertexBuffer;
    protected FloatBuffer colorBuffer;
    private int drawType = GLES30.GL_TRIANGLES;


    private Triangle(FloatBuffer vertex, FloatBuffer color, int type) {
        vertexBuffer = vertex;
        colorBuffer = color;
        setDrawType(type);
    }

    private Triangle(Builder builder) {
        this(builder.vertexBuffer, builder.colorBuffer, builder.drawType);
    }

    public static Builder newBuilder() {
        return new Builder();
    }

    public void setDrawType(int type) {
        if (type == GLES30.GL_TRIANGLES || type == GLES30.GL_TRIANGLE_STRIP || type == GLES30.GL_TRIANGLE_FAN) {
            drawType = type;
        } else {
            Log.e(TAG, "DrawType not correct, should be  GLES30.GL_TRIANGLES ，GLES30.GL_TRIANGLE_STRIP or GLES30.GL_TRIANGLE_FAN！");
        }
    }

    @Override
    public void onCreate(Context context) {
        vertexShaderCode = ShaderUtil.loadFromAssetsFile("shader/triangle.vert", context.getResources());
        fragmentShaderCode = ShaderUtil.loadFromAssetsFile("shader/triangle.frag", context.getResources());
        programId = ShaderUtil.createProgram(vertexShaderCode, fragmentShaderCode);
        initLocation();
    }

    private void initLocation() {
        LOCATION_VERTEX = GLES30.glGetAttribLocation(programId, "position");
        LOCATION_COLOR = GLES30.glGetAttribLocation(programId, "color");
        LOCATION_MATRIX_P = GLES30.glGetUniformLocation(programId, "projection");
        LOCATION_MATRIX_V = GLES30.glGetUniformLocation(programId, "view");
        LOCATION_MATRIX_M = GLES30.glGetUniformLocation(programId, "model");
    }

    @Override
    public void onDraw() {
        if (programId == 0) {
            Log.e(TAG, "Program id is 0 ,may not init");
            return;
        }
        GLES30.glUseProgram(programId);
        GLES30.glEnableVertexAttribArray(LOCATION_VERTEX);
        GLES30.glEnableVertexAttribArray(LOCATION_COLOR);
        GLES30.glUniformMatrix4fv(LOCATION_MATRIX_M, 1, false, mMatrix, 0);
        GLES30.glUniformMatrix4fv(LOCATION_MATRIX_V, 1, false, vMatrix, 0);
        GLES30.glUniformMatrix4fv(LOCATION_MATRIX_P, 1, false, pMatrix, 0);

        GLES30.glVertexAttribPointer(LOCATION_VERTEX, VERTEX_SIZE, GLES30.GL_FLOAT, false, FLOAT_SIZE * VERTEX_SIZE, vertexBuffer);
        GLES30.glVertexAttribPointer(LOCATION_COLOR, COLOR_SIZE, GLES30.GL_FLOAT, false, FLOAT_SIZE * COLOR_SIZE, colorBuffer);

        GLES30.glDrawArrays(drawType, 0, vertexBuffer.capacity() / VERTEX_SIZE);

        GLES30.glDisableVertexAttribArray(LOCATION_VERTEX);
        GLES30.glDisableVertexAttribArray(LOCATION_COLOR);
    }

    @Override
    public void onDestroy() {

    }

    public static final class Builder {
        private FloatBuffer vertexBuffer;
        private FloatBuffer colorBuffer;
        private int drawType;

        private Builder() {
        }

        public Builder vertexBuffer(float[] vertex) {
            vertexBuffer = ByteBuffer.allocateDirect(vertex.length * FLOAT_SIZE)
                    .order(ByteOrder.nativeOrder())
                    .asFloatBuffer().put(vertex);
            vertexBuffer.flip();
            return this;
        }

        public Builder colorBuffer(float[] color) {
            colorBuffer = ByteBuffer.allocateDirect(color.length * FLOAT_SIZE)
                    .order(ByteOrder.nativeOrder())
                    .asFloatBuffer().put(color);
            colorBuffer.flip();
            return this;
        }

        public Builder vertexBuffer(FloatBuffer vertex) {
            vertexBuffer = vertex;
            return this;
        }

        public Builder colorBuffer(FloatBuffer color) {
            colorBuffer = color;
            return this;
        }

        public Builder drawType(int drawType) {
            this.drawType = drawType;
            return this;
        }

        public Triangle build() {
            if (*//*vertex.length <= 9 ||*//* vertexBuffer.capacity() % 3 != 0) {
                throw new IllegalArgumentException("vertex length not correct! ");
            }
            if (*//*color.length <= 9 ||*//* colorBuffer.capacity() % 3 != 0) {
                throw new IllegalArgumentException("vertex length not correct! ");
            }
            if (drawType != GLES30.GL_TRIANGLES && drawType != GLES30.GL_TRIANGLE_STRIP && drawType != GLES30.GL_TRIANGLE_FAN) {
                throw new IllegalArgumentException("vertex length not correct! ");
            }

            return new Triangle(this);
        }
    }*/
}
